package com.voxel.sense.infomsg.view.menu;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.com.common.view.dialog.BaseDialog;
import com.voxel.sense.infomsg.App;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.interfaces.listener.onMyDialogItemClickListener;
import com.voxel.sense.infomsg.utils.GlideUtils;
import com.voxel.sense.infomsg.view.MenuBean;

import java.util.List;

/**
 * D多选菜单
 */


public class MultipleSelectMenuDialog extends BaseDialog {
    private ListView listView;
    private List<MenuBean> mDataList;
    private LayoutInflater mLayoutInflater;
    private OnItemClickListener onItemClickListener;
    private String title;
    private int mTitleGravity, mItemGravity;
    boolean mAddSelectAllItem = false; //一个选中全部的item
    private boolean mIsVisibleIcon = false;//是否显示item图标
    private int mAllItemIconRes;
    private int mTitleIconRes;
    private MultipleSelectAdapter mMultipleSelectAdapter;
    private onMyDialogItemClickListener mMyItemClickListener;


    /**
     * @param activity
     * @param menuList
     * @param title            标题
     * @param titleRes         标题图标
     * @param SelectAllItemRes 添加选中全部的图标
     * @param addSelectAllItem 是否添加一个选中全部的item
     * @param visibleIcon      icon是否可见
     */
    public MultipleSelectMenuDialog(Context activity, List<MenuBean> menuList, String title, int titleRes, int
            SelectAllItemRes, boolean addSelectAllItem, boolean visibleIcon) {
        this(activity, menuList, title, titleRes, SelectAllItemRes, addSelectAllItem,
                visibleIcon, Gravity.LEFT | Gravity.CENTER_VERTICAL, Gravity.LEFT | Gravity.CENTER_VERTICAL);
    }

    public MultipleSelectMenuDialog(Context activity, List<MenuBean> menuList, String title, int titleRes, int SelectAllItemRes,
                                    boolean addSelectAllItem, boolean visibleIcon, int itemGravity, int titleGravity) {
        super(activity, R.style.translucentDialog);
        this.mDataList = menuList;

        this.mLayoutInflater = LayoutInflater.from(activity);
        this.title = title;
        this.mItemGravity = itemGravity;
        this.mTitleGravity = titleGravity;
        this.mAddSelectAllItem = addSelectAllItem;
        this.mAllItemIconRes = SelectAllItemRes;
        this.mIsVisibleIcon = visibleIcon;
        this.mTitleIconRes = titleRes;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_mult_select_menu);
        listView = (ListView) findViewById(R.id.listview_dialog_menu);
        TextView tvTitle = (TextView) findViewById(R.id.tv_dialog_title);
        View ll_title = findViewById(R.id.ll_title);
        if (!TextUtils.isEmpty(title)) {
            ll_title.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
            findViewById(R.id.dialog_title_line).setVisibility(View.VISIBLE);
            tvTitle.setGravity(mTitleGravity);
//            if (mTitleIconRes != 0) {
                ImageView imageView = findViewById(R.id.iv_title_icon);
                imageView.setVisibility(View.VISIBLE);
                imageView.setImageDrawable(App.getInstance().getDrawable(mTitleIconRes));
//                GlideUtils.GlideLoadPicNoDefault(getContext(), mTitleIconRes, imageView);
//            }

        } else {
            ll_title.setVisibility(View.VISIBLE);
            tvTitle.setVisibility(View.GONE);
            findViewById(R.id.dialog_title_line).setVisibility(View.GONE);
        }
        mMultipleSelectAdapter = new MultipleSelectAdapter(mDataList, getContext(),
                R.layout.dialog_multiple_select_menu_item, mAddSelectAllItem, mAllItemIconRes, mIsVisibleIcon);
        if (mMyItemClickListener != null) {
            mMultipleSelectAdapter.setOnMyItemClickListener(mMyItemClickListener);
        }
        listView.setAdapter(mMultipleSelectAdapter);
        findViewById(R.id.linear_outside).setOnTouchListener(
                new OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (canceledOnTouchOutside)
                            dismiss();
                        return false;
                    }
                });
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onItemClickListener != null) {
                    dismiss();
                    onItemClickListener.onItemClick(parent, view, position, id);
                }
            }
        });
    }

    //设置集合数据
    public void setDataList(List<MenuBean> list) {
        if (mMultipleSelectAdapter != null) {
            mDataList = list;
            mMultipleSelectAdapter.setList(mDataList);
        }

    }

    //设置集合数据
    public List<MenuBean> getDataList() {
        return mDataList;

    }

    public void setOnItemClickListener(onMyDialogItemClickListener onItemClickListener) {
        this.mMyItemClickListener = onItemClickListener;
    }

    public List<MenuBean> getSelectListData() {
        if (mMultipleSelectAdapter == null) {
            return null;
        }
        return mMultipleSelectAdapter.getSelectListData();
    }


    private boolean canceledOnTouchOutside = true;

    @Override
    public void setCanceledOnTouchOutside(boolean cancel) {
        this.canceledOnTouchOutside = cancel;
    }
}
